<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html>
 <head>
    <title>生产退料入库批次管理</title>
    <!-- import css and js -->
	<%@ include file="/common/meta.jsp" %>
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript">
		var basic_Url="${ctx}/yd/logic/materialreturn";
		var ItemStateList = ItemStateData.Rows;
		var item = eval("("+'${mItemBatchDO}'+")");
		var mareturnitemid = item.mareturnitemid;
		var code = item.code;
		var bd = '${itemBatchDOs}';
		//var delBd = '${delItemBatchDOs}';
 		//alert("bd:"+bd);
// 		function createSearch(){
// 			init(1,6,'100,100,100,100,100,100','10%');
// 			//第一行的数据
// 			addCommon(0,0,"lable","物料编码：");
// 			addCommon(0,1,"txtlike","code");
// 			addCommon(0,2,"lable","物料名称：");
// 			addCommon(0,3,"txtlike","name");
// 			addCommon(0,4,"lable","规格型号：");
// 			addCommon(0,5,"txtlike","spec");
// 		}
		
// 		function searchAP(){
// 			multipleSearch("#table1",true,"",basic_Url+"/itemlist.do?componentSequenceId="+componentSequenceId);
// 		}
		var grid;
		$(function(){
			var column1 =  [
							  { display: 'id', name: 'id',hide:true,width:1},
		               { display: '物料id', name: 'materialsid',hide:true,width:1},
		               { display: '<font color="gray">编码</font>', name: 'code',width: 120},
		               { display: '<font color="gray">名称</font>', name: 'name',width: 120},
		               { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
		               { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
		               { display: '<font color="gray">基本单位数量</font>', name: 'jquantity',width: 120, render: function (item){ 
		            	   if(item.jquantity == null ||item.jquantity == "" || parseFloat(item.jquantity) == 0){
		                		  return '0.0';
		                	} else {
                          		return item.jquantity;
		                	}
                      }},
		               { display: '批次<font color="red">*</font>', name: 'batch' ,width: 120,editor: { type: 'text'}},
		               { display: '状态<font color="red">*</font>', name: 'status', width: 120,editor: { type: 'select', data: ItemStateList, valueColumnName: 'ItemStateID', displayColumnName: 'ItemStateName' }, render: function (item)
		                   {
		                   for (var i = 0; i < ItemStateList.length; i++)
		                   {
		                       if (ItemStateList[i]['ItemStateID'] == item.status){
		                     	  return ItemStateList[i]['ItemStateName'];
		                       }
		                   }
		                   return item.status;
		               }},
		           	   { display: '单位<font color="red">*</font>', name: 'unit', width: 120
// 		            	   ,editor : {
// 		            	 	   type:'selecturl',dataItem:'dataItem',path:'${ctx}/yd/basic/measurementUnit/unitlist.do',valueColumnName: 'unitname', displayColumnName: 'unitname'}, render: function (item)
// 		                       {
// 		            	 		  var dataitem = $.parseJSON($("#dataItem").val());
// 		            	 		  if(dataitem!=null){
// 		            	 			 for (var i = 0; i < dataitem.length; i++)
// 		                             {
// 		                                 if (dataitem[i]['unitname'] == item.unit&&dataitem[i]['materialscode']==item.code){
// 		                                	 grid.updateCell('unitconvert', dataitem[i]['unitconvert'], item);
// 		                                     return dataitem[i]['unitname'];
// 		                                 } 
// 		                             }
// 		            	 		  }
// 		                           return item.unit;
// 		                       }
		            	 	   },
				        { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
		               { display: '退料数量<font color="red">*</font>', name: 'quantity',width: 150,editor : {type :'float'}, render: function (item)
	                       {
		            	   		if(item.quantity!=''&&item.quantity!=null&&item.unitconvert!=null&&item.unitconvert!=''){
		            	   			grid.updateCell('jquantity', item.quantity*item.unitconvert, item);
		            	   		} 
		            	   		if(item.quantity == null ||item.quantity == "" || parseFloat(item.quantity) == 0){
			                		  return '0.0';
			                	} else {
	                          		return item.quantity;
			                	}
	                       }}
			                  ];
			var column2 =  [
						{ display: 'id', name: 'id',hide:true,width:1},
		               { display: '物料id', name: 'materialsid',hide:true,width:1},
		               { display: '<font color="gray">编码</font>', name: 'code',width: 120},
		               { display: '<font color="gray">名称</font>', name: 'name',width: 120},
		               { display: '<font color="gray">型号</font>', name: 'spec' ,width: 270},
		               { display: '<font color="gray">基本单位</font>', name: 'junit' ,width: 100},
		               { display: '<font color="gray">基本单位数量</font>', name: 'jquantity',width: 120},
		               { display: '批次<font color="red">*</font>', name: 'batch' ,width: 120},
		               { display: '状态<font color="red">*</font>', name: 'status', width: 120,
		                	  render: function (item){
		                		  var status=item.status;
		                		  if(status=="1"){
		                			  return "一等品";
		                		  }
		                		  if(status=="2"){
		                			  return "次品";
		                		  }
		                		  if(status=="3"){
		                			  return "旧件";
		                		  }
		                	  }},
		           	   { display: '单位<font color="red">*</font>', name: 'unit', width: 120},
				        { display: '<font color="gray">单位换算率</font>', name: 'unitconvert',width:80},
		               { display: '退料数量<font color="red">*</font>', name: 'quantity',width: 150}
			                  ];
			//var colunm3 = column1;                 
// 		    if('${billstatus}' != "" && '${billstatus}' != "0"){
// 		    	colunm3 = column2;    
// 		    }
 if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
	 grid = $("#table1").ligerGrid({
	        columns: column2, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?mareturnitemid="+mareturnitemid+"&code="+code+"&itembatch="+encodeURIComponent(bd),
	                      width: "100%", height:"98%"
	                  });
 }else{
	 grid = $("#table1").ligerGrid({
	        columns: column1, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?mareturnitemid="+mareturnitemid+"&code="+code+"&itembatch="+encodeURIComponent(bd),
	                      width: "100%", height:"98%",
	                      toolbar: { items: [
	                                         { text: '添加', click: insert, icon: 'add' },
	                                         { line: true },
	                                         { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
	                                         ]
	                                         }
	                  });
 }
				
				mareturnitemid ='';
				code ='';
			$("#pageloading").hide();
		
		
		});
		
		function getSelected(){
			var rows = grid.getCheckedRows();
			 return rows;
		}
		
		function Close(){
			CloseDialog();
		}
		function insert(){
			if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
				return false;
			}else{
				var manager = $("#table1").ligerGetGridManager();
				manager.addRow2({
					mareturnitemid:item.mareturnitemid,
					materialsid: item.materialsid,
		    		code: item.code,
		    		name: item.name,
		    		spec: item.spec,
		    		junit : item.junit,
		    		unit : item.unit,
		    		unitconvert : item.unitconvert
				});
			}
		}
		function deleteRow()
		{ if('${billstatus}' != "add" && '${billstatus}' != "tijiao"){
			return false;
		}else{
			grid.deleteSelectedRow();
		}
		}
		var ba=true;
		var num = 0;
		var jnum = 0;
		function yz(){
			var data = grid.getData();
			if (data != null && data.length > 0) {
				jnum = 0;
				num = 0;
				for ( var i = 0; i < data.length; i++) {
					if(data[i].batch==undefined ||data[i].batch.replace(/(^\s*)|(\s*$)/g, "")=="" ||data[i].batch==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的批次没有填写");
	 					return false;
	 				}
	 				if(data[i].status=="" ||data[i].status==undefined ||data[i].status==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的状态没有填写");
	 					return false;
	 				}
	 				for ( var j = 0; j < data.length; j++) {
	 					if(i != j){
	 						if(data[i].code==data[j].code && data[i].batch==data[j].batch && data[i].status==data[j].status){
	 							ba = false;
	 							$.dialog.alert("第"+(i+1)+"行与第"+(j+1)+"行存在相同的批次与等级!");
	 							return false;
	 						}
	 					}
	 				}
	 				jnum += parseFloat(data[i].jquantity);
	 				num  += parseFloat(data[i].quantity);
	 				if (isNaN(data[i].quantity) ||data[i].quantity==undefined ||data[i].quantity==null) {
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的数量没有填写");
	 					return false;
	 				}
	 				if (data[i].quantity <= 0) {
	 					$.dialog.alert("您第" + (i+1) + "行的入库数量不能小于等于0");
	 					ba = false;
	 					return false;
	 				}
	 			}
			}
				ba=true;
		}
	</script>
	
  </head>
 <body style="padding:10px">  
       <div id="layout1">
       <div id="layout1">
<!--           <div class="panelBar"> -->
<!-- 	            <ul class="toolBar"> -->
<!-- 		           <li><a class="add" onclick="insert()"><span>新增</span></a></li> -->
<!-- 		       		<li><a class="delete" onclick="deleteRow()"><span>删除</span></a></li> -->
<!--                </ul> -->
<!--           </div> -->
          <div id="pageHeader" class="pageHeader">
               <div class="searchBar" id="multipleSearchDialog">
	                <table class="table" width="100%" id="searchHeader" >  
                        <tbody id="newbody">
                        </tbody>
 	                </table>
<!--                     <div class="subBar"> -->
<!-- 			             <ul> -->
<!-- 				             <li><div class="buttonActive"><div class="buttonContent"><button type="submit" id="btnSearch" onclick="insert()">添加</button></div></div></li> -->
<!--  				             <li><a class="button" onclick="clearSearch()"><span>删除</span></a></li>  -->
<!-- 			             </ul> -->
<!-- 	                </div> -->
               </div>
          </div>
      </div>
      <div position="center" id="table1">
      </div>
      <div class="clear"></div>
   </div>
    </body>
</html>